package com.market.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.market.sys.entity.Comment;
import com.market.sys.entity.SubComment;
import com.market.sys.entity.User;
import com.market.sys.mapper.CommentMapper;
import com.market.sys.mapper.SubCommentMapper;
import com.market.sys.mapper.UserMapper;
import com.market.sys.service.ICommentService;
import com.market.sys.util.EmptyUtil;
import com.market.sys.util.TimeUtil;
import com.market.sys.vo.CommentVO;
import com.market.sys.vo.SubCommentVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.market.sys.util.Constants.DOWNLOAD_URL;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private SubCommentMapper subCommentMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<CommentVO> fetchCommentList(Integer thingId, Integer state) {
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getThingId, thingId);
        if (!EmptyUtil.isNumberEmpty(state)){
            lambdaQueryWrapper.eq(Comment::getState, state);
        }
        List<Comment> commentList = this.baseMapper.selectList(lambdaQueryWrapper);
        return getCommentVOS(commentList);
    }

    @Override
    public List<CommentVO>  fetchCommentList(Map<String, Object> paramMap) {
        int pageNum = (int)(paramMap.get("pageNum"));
        int pageSize = (int)(paramMap.get("pageSize"));
        Page<Comment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comment::getThingId, String.valueOf(paramMap.get("thingId")));
        if (!EmptyUtil.isStringEmpty(String.valueOf(paramMap.get("state")))){
            lambdaQueryWrapper.eq(Comment::getState, String.valueOf(paramMap.get("state")));
        }
        Page<Comment> commentPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return getCommentVOS(commentPage.getRecords());
    }

    @Override
    public CommentVO fetchCommentDetail(Integer commentId) {
        Comment comment = query().eq("id", commentId).one();
        return convertCommentToVO(comment);
    }

    private List<CommentVO> getCommentVOS(List<Comment> comments){
        List<CommentVO> commentVOList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVOList.add(convertCommentToVO(comment));
        }
        return commentVOList;
    }

    private CommentVO convertCommentToVO(Comment comment){
        CommentVO commentVO = new CommentVO();
        comment.setTime(TimeUtil.completeGetTimeStamp(comment.getTime()));

        // 查询用户
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getId, comment.getUserId());
        User user = userMapper.selectOne(userLambdaQueryWrapper);

        BeanUtil.copyProperties(comment, commentVO, false);
        commentVO.setNickName(user.getNickName());
        if (!EmptyUtil.isStringEmpty(user.getAvatar())){
            commentVO.setAvatar(DOWNLOAD_URL + user.getAvatar());
        }

        LambdaQueryWrapper<SubComment> subCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        subCommentLambdaQueryWrapper.eq(SubComment::getParentCommentId, comment.getId());
        if (!EmptyUtil.isNumberEmpty(comment.getState())){
            subCommentLambdaQueryWrapper.eq(SubComment::getState, comment.getState());
        }
        List<SubComment> subComments = subCommentMapper.selectList(subCommentLambdaQueryWrapper);
        if (subComments.size() >= 3){
            subComments.subList(0, 2);
        }

        List<SubCommentVO> subCommentVOList = new ArrayList<>();
        for (SubComment subComment : subComments) {
            SubCommentVO subCommentVO = new SubCommentVO();

            // 查询用户
            LambdaQueryWrapper<User> userLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper2.eq(User::getId, subComment.getUserId());
            User subUser = userMapper.selectOne(userLambdaQueryWrapper2);

            if (!EmptyUtil.isStringEmpty(subUser.getAvatar())){
                subCommentVO.setAvatar(DOWNLOAD_URL + subUser.getAvatar());
            }
            BeanUtil.copyProperties(subComment, subCommentVO, false);
            subCommentVO.setNickName(subUser.getNickName());
            subCommentVOList.add(subCommentVO);
        }
        commentVO.setSubCommentList(subCommentVOList);
        return commentVO;
    }
}
